define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form) {

  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: 'wx/wxproduct/index',
          add_url: 'wx/wxproduct/add',
          add2_url: 'wx/wxproduct/yd',
          edit_url: 'wx/wxproduct/edit',
          del_url: 'wx/wxproduct/del',
          multi_url: 'wx/wxproduct/multi',
          import_url: 'wx/wxproduct/import',
          copys_url: 'wx/wxproduct/copys',
          copy_url: 'wx/wxproduct/copy',
          table: 'wxproduct',
        }
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: 'id',
        sortName: 'id',
        columns: [
          [
            {checkbox: true},
            {field: 'id', title: __('Id')},
            // {field: 'code', title: __('Code'), editable: true, sortable: true},
            {field: 'type', title: __('Type'), searchList: typeList, formatter: Table.api.formatter.normal},
            {field: 'name', title: __('Name'), editable: true, operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
            {
              field: 'wxproids_text',
              title: __('半成品'),
              operate: false,
              visible:false,
              formatter: function (value, row, index) {
                var html = [];
                var arr = value.split(',');
                $.each(arr, function (i, value) {
                  value = value === null ? '' : value.toString();
                  if (value == '') return true;
                  html.push('<span class="label label-primary">' + value + '</span>');
                });
                return html.join(' ');
              }
            },
            {field: 'ministrial', title: __('Ministrial'), operate: 'BETWEEN', editable: true, sortable: true},
            {field: 'unit', title: __('Unit'), searchList: unitList, formatter: Table.api.formatter.normal},
            {field: 'kg', title: __('Kg'), editable: true, sortable: true},
            {field: 'weigh', title: __('Weigh'), editable: true, sortable: true},
            // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
            {
              field: 'operate',
              width: "150px",
              title: __('Operate'),
              table: table,
              buttons: [
                {
                  name: 'copy',
                  icon: 'fa fa-copy',
                  title: __('复制产品'),
                  extend: 'data-toggle="tooltip"',
                  classname: 'btn btn-xs btn-success btn-copy',
                  url: 'wx/wxproduct/copy',
                  hidden:function (row) {
                    if(!row.wxproids_text)
                    {
                      return true;
                    }
                    return false;
                  }
                }
              ],
              events: Table.api.events.operate,
              formatter: Table.api.formatter.operate
            },
          ]
        ]
      });

      // 为表格绑定事件
      Table.api.bindevent(table);
      // 批量编辑按钮事件
      $(document).on("click", ".btn-config", function () {
        Fast.api.open('wx/wxproduct/config', '分类配置', $(this).data() || {});
      });
      $(document).on("click", ".btn-config3", function () {
        Fast.api.open('wx/wxproduct/config3', '分类配置', $(this).data() || {});
      });
      // 批量编辑按钮事件
      $(document).on("click", ".btn-config2", function () {
        Fast.api.open('wx/wxproduct/config2', '分类配置', $(this).data() || {});
      });
    },
    add: function () {
      $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {
        $(".yangliuType").data("params", function (obj) {
          return {custom: {type: $('#c-unit').val()}};
        });
        Form.events.selectpage(obj);
      });
      Controller.api.bindevent();
    },
    config3: function () {
      Form.api.bindevent($("form[role=form]"), function (data, ret) {
        console.log(3);
      },function (data,ret) {
        console.log(2);
      },function (data,ret) {
        console.log(1);
      });
      Controller.api.bindevent();
    },
    edit: function () {
      Controller.api.bindevent();
    },
    copy: function () {
      Controller.api.bindevent();
    },
    config: function () {
      Controller.api.bindevent();
    },
    config2: function () {
      Form.api.bindevent($("form[role=form]"), function (data, ret) {
        window.location.reload();
      });
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"), function (data, ret) {
          // Toastr.success(ret.msg);//成功
        },function (data,ret) {
          if(data)
          {
            layer.open({
              type: 1,
              title:'小学上下册组合',
              skin: 'layui-layer-rim', //加上边框
              area: ['570px', '440px'], //宽高
              content: data
            });
          }
        },function (data,ret) {
          Toastr.success(ret.msg);//成功
        });
      }
    }
  };
  return Controller;
});